﻿using KzwgRoomTester;
using System.Net.Sockets;

class PacketSender : IDestroyable
{
    public long sendedBytes { get; protected set; }

    public bool destroyed { get; protected set; }
    public bool pendingDestroy { get { return false; } }

    private IConnection m_conn;

    public PacketSender(IConnection conn)
    {
        m_conn = conn;
    }

    public void Send(Packet packet)
    {
        if (destroyed || !m_conn.IsConnected) return;

        packet.Flush();
        var bytes = packet.bytes;

        if (bytes.Length > Packet.maxSize)
        {
            Logger.LogException("PacketSender::Send: Toooooooooo large packet size, discard!! Packet: [ID: {0}, curSize: {1}]", packet.ID, bytes.Length);
            packet.Destroy();
            return;
        }

        m_conn.Send(bytes, 0, bytes.Length);
    }



    public void Destroy()
    {
        if (destroyed) return;
        destroyed = true;
        m_conn.Close();
        m_conn = null;
    }
}